/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  *
  * COPYRIGHT(c) 2016 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "stdport.h"
#include "rtos.h"


/* USER CODE BEGIN Includes */     

/* USER CODE END Includes */

/* Variables -----------------------------------------------------------------*/

/* USER CODE BEGIN Variables */

static xQueueHandle pxTaskQueue[OS_NUM_TASKS];//message queue handle

/* USER CODE END Variables */

/* Function prototypes -------------------------------------------------------*/

/* USER CODE BEGIN FunctionPrototypes */
Status_Type OS_Wait_Message(Task_Type task_id,Message_Type * msg, Tick_Type timeout)
{
    //Need check the pointer valid or not, freeRTOS doesn't do this.
    if (pxTaskQueue[task_id]== NULL) return (E_ERROR);
    if(pdfalse == xQueueReceive(pxTaskQueue[task_id],(void *)msg,timeout))
    {
        return (E_ERROR);     /* else return there was no message */
    }
    else
    {
        return (E_OK);     /*OK */
    }
}

Message_Type Build_Message(uint16_t id, int16_t data)
{
   Data_Message_T temp_msg = {0};
   temp_msg.parts.msg = id;
   temp_msg.parts.data = data;
   return (temp_msg.all);
}

Status_Type OS_Send_Message(Task_Type task_id, Message_Type msg)
{
   Message_Type xmsg = msg;

   //Need check the pointer valid or not, freeRTOS doesn't do this.
   if (pxTaskQueue[task_id]== NULL) return (E_ERROR);
   if(errQUEUE_FULL == xQueueSend(pxTaskQueue[task_id], ( void * ) &xmsg, 0))
   {
       return (E_ERROR);       /* Queue full */
   }
   else
   {
        return (E_OK);     /* else return there was no message */
   }
   
}

/* USER CODE END FunctionPrototypes */
extern void OS_Create_Message(void)
{
    int i = 0 ;
    
    for(i = 0 ; i < OS_NUM_TASKS ; i ++)
    {
        pxTaskQueue[i] = xQueueCreate( 5 , TASK_MSG_SIZE );/*create message queues for each task*/
    }
}
/* Hook prototypes */

/* USER CODE BEGIN Application */
     
/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
